# 检测所有能力集中，是否有：？车窗门模块支持只读不可控。
# 因为这种情况 x9e 目前代码是不允许的，目前代码只要支持门模块则必须可控。
# 查找车窗能力下的 openModeCapability 是否 = 1
import pymysql
import re
import json


def getCapability():
    # SQL 查询语句
    sql = "SELECT * FROM t_vehicle_capability v WHERE 1=1"
    # 执行SQL语句
    cursor.execute(sql, ())
    # 获取一条记录列表
    return cursor.fetchall()


# 建立mysql链接
db = pymysql.connect(host='gz-cdb-o43ukrr9.sql.tencentcdb.com',
                     port=60652,
                     user='dskj',
                     passwd='Dskj!2019',
                     db='vehicle_capability',
                     charset='utf8')
# db = pymysql.connect(host='10.2.5.9',
#                      port=3306,
#                      user='tdsql_rw',
#                      passwd='guangqi@2018',
#                      db='vehicle_capability',
#                      charset='utf8')
# db = pymysql.connect(host='10.123.2.10',
#                      port=3306,
#                      user='pre_read',
#                      passwd='123^54Read',
#                      db='vehicle_capability',
#                      charset='utf8')

cursor = db.cursor(cursor=pymysql.cursors.DictCursor)  # 使用cursor()方法获取操作游标

num = 0
resultDict = dict(())
capabilities = getCapability()
for capability in capabilities:
    jsStr = capability['capability']
    js = json.loads(jsStr)
    remoteCapability = js['remoteCapability']
    # print(remoteCapability)
    if 'windowCapabilities' in remoteCapability:
        windowCapabilities = remoteCapability['windowCapabilities']
        if len(windowCapabilities) > 0:
            for windowCapability in windowCapabilities:
              # 返回车窗只读的能力集 开始
                # if 'controlCapability' in windowCapability and 'openModeCapability' in windowCapability:
                #     controlCapability = windowCapability['controlCapability']
                #     if 1 == controlCapability:
                #         openModeCapability = windowCapability['openModeCapability']
                #         if 1 == openModeCapability and capability['vehicle_type'] not in resultDict:
                #             print(capability['vehicle_type']+' read only')
                #             resultDict[capability['vehicle_type']
                #                        ] = capability['vehicle_type']
              # 返回车窗只读的能力集 结束
                if 'controlCapability' in windowCapability and 'optionCapability' in windowCapability:
                    controlCapability = windowCapability['controlCapability']
                    if 1 == controlCapability:
                        optionCapability = windowCapability['optionCapability']
                        if optionCapability is not None and capability['vehicle_type'] not in resultDict:
                            print(capability['vehicle_type'] +
                                  ' has: '+str(optionCapability)+' openModeCapability: '+str(windowCapability['openModeCapability']))
                            resultDict[capability['vehicle_type']
                                       ] = capability['vehicle_type']
